import axios from 'axios';
import {Modal} from 'antd';
import router from "next/router";
import {basePath} from './tool';
axios.interceptors.request.use((config) => {
  if (typeof window !== 'undefined') {
    const token = window.localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `${token}`;
    }
  }
  return config;
},
() => {
  throw new Error('发起请求出错');
});

export const request = async (url,params)=>{
    return new Promise((resolve,reject)=>{
        axios({
            method: 'post',
            url: url,
            data: params,
          })
          .then((res) => {
            // debugger
            //登录过期跳转登录
            if(res.data.code === 401){
               localStorage.removeItem('token')
               router.push('/account/login')
             
            } 
            resolve(res.data )
            
          })
          .catch((error) => {
            reject( error );
          });
    })
}